package com.bc.mcode.controller;

import com.bc.mcode.collect.Maps;
import com.bc.mcode.core.ClockManager;
import com.bc.mcode.core.ClockResponse;
import com.bc.mcode.model.ClockVO;
import com.bc.mcode.service.ClockVOService;
import com.bc.mcode.util.BaseController;
import com.bc.mcode.util.StringX;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/clock")
public class InterfaceController extends BaseController {

    @Resource
    private ClockVOService clockVOService;

    public boolean isTime(String str){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            Date date = format.parse(str);
        } catch (ParseException e){
            return false;
        }
        return true;
    }

    public long getTime(String str){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            Date date = format.parse(str);
            return date.getTime();
        } catch (ParseException e){
            return -1;
        }
    }

    public boolean isUrl(String url){
        Pattern p = Pattern.compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+$");
        return p.matcher(url).matches();
    }

    @RequestMapping("/responseTest")
    @ResponseBody
    public Map<String, Object> responseTest(String mark ,String data){
        Map<String, Object> resultMap = Maps.newHashMap();
//        System.out.println("====responseTest mark:" + mark+",data:"+data);
        resultMap.put("flg", true);
        return resultMap;
    }



    /**
     * 增加闹钟提醒 条目
     * @param mark
     * @param data
     * @param time
     * @param url
     * @return
     * @eg: http://localhost:8085/mclock/clock/addClock?mark=test&data=ihaveadream&time=2018-10-25 13:50:10&url=http://localhost:8085/mclock/clock/responseTest
     */
    @RequestMapping("/addClock")
    @ResponseBody
    public Map<String, Object> addClock(String mark, String data, String time, String url) {
        Map<String, Object> resultMap = Maps.newHashMap();
        boolean flg = false;
        String msg = "";
        Integer clockId = null;
        log.info("mark:" + mark + ",data:" + data + ",time:" + time+",url:"+url);
         //        mark:service,data:2null,time:2019-01-29,url:http://36.49.92.140:8081/culture_practice_core/callback/mclock
        if(mark !=null && !"".equals(mark) && data != null&& !"".equals(data) && time != null && !"".equals(time) && url !=null && !"".equals(url)){
            long dataTime = getTime(time);
            if(dataTime!=-1){
                if (dataTime>-1&&isUrl(url)){
                    long nowTime = new Date().getTime();
                    if(nowTime < dataTime){
                        ClockVO clockVO = new ClockVO();
                        clockVO.setClockState(1);
                        clockVO.setClockTime(time);
                        clockVO.setClockMark(mark);
                        clockVO.setClockData(data);
                        clockVO.setClockIntime(StringX.getCurrentDate());
                        clockVO.setClockUrl(url);
                        //Map<String, Object> map = clockVOService.add(clockVO);
                        Map<String, Object> map = new HashMap<>();
                        if((Boolean) map.get("flg")){
                            ClockManager.getInstance().addClockVO(clockVO);
                            flg = true;
                            clockId = clockVO.getClockId();
                        }else{
                            msg = "保存clockVO对象失败，请与开发联系 !";
                        }
                    }else{
                        msg = "参数非法: 时间必须 > 当前时间! ";
                    }
                }else{
                    msg = "参数非法 eg：?time=2018-10-25 13:50:10&url=http://localhost:8085/mclock/system/Clock.";
                }
            }else{
                msg = " 时间参数非法 eg:time=2018-10-25 13:50:10!";
            }
        }else{
            msg = " String mark, String data, String time, String url 参数必传!";
        }
        resultMap.put("flg", flg);
        resultMap.put("errorMsg", msg);
        resultMap.put("clockId", clockId);
        log.info("errorMsg:" + msg);
        return resultMap;
    }

    /**
     * 删除提醒条目
     * @param clockId
     * @param clockTime
     * @eg: http://localhost:8085/mclock/clock/deleClock?clockId=1&clockTime=2018-10-25 13:50:10
     * @return
     */
    @RequestMapping("/deleClock")
    @ResponseBody
    public Map<String, Object> deleClock(Integer clockId, String clockTime){
        log.debug("delete clockId:" + clockId + ",clockTime:" + clockTime);
        ClockManager.getInstance().deleClockVO(clockId,clockTime);
        ClockResponse.getInstance().deleClockVO(clockId);
        //Map<String, Object> map = clockVOService.deleteByPrimaryKey(clockId);
        //return map;
        return null;
    }

}
